package com.clay.appointment.controller;

import com.clay.appointment.common.page.PageInfo;
import com.clay.appointment.common.response.Result;
import com.clay.appointment.entity.hospital.HospitalSet;
import com.clay.appointment.service.HospitalSetService;
import com.clay.appointment.vo.hospital.HospitalSetQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author : clay
 * @Description:
 * @Date: Create in 17:08 2021/7/20
 * @Modified By:
 */
@Api(tags = "医院设置管理接口")
@RestController
@RequestMapping("/hospitalSet")
@Slf4j
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;
//    private static final ThreadLocal<Long> THREAD_LOCAL = new ThreadLocal<>();


    @ApiOperation("查询医院设置表的所有信息")
    @GetMapping("/findAll")
    public Result<List<HospitalSet>> findAllHospitalSet() {
        return Result.success(hospitalSetService.findAllHospitalSet());
    }


    @ApiOperation("根据id删除表中数据")
    @DeleteMapping("/{id}")
    public Result removeHospitalSet(@ApiParam(value = "id", name = "主键") @PathVariable("id") Long id) {
        return Result.success(hospitalSetService.removeHospitalSetById(id));
    }


    @ApiOperation("条件查询带分页")
    @PostMapping("/findPageHospitalSet")
    public Result<PageInfo<HospitalSet>> findPageHospitalSet(@RequestBody(required = false) HospitalSetQueryVO hospitalSetQueryVO) {
        PageInfo<HospitalSet> page = hospitalSetService.findPageHospitalSet(hospitalSetQueryVO);
        return Result.success(page);
    }


    @ApiOperation("添加医院设置")
    @PostMapping("/saveHospitalSet")
    public Result saveHospitalSet(@RequestBody(required = false) HospitalSet hospitalSet) {
        hospitalSetService.saveHospitalSet(hospitalSet);

        return Result.success();
    }

    @ApiOperation("根据id获取医院设置")
    @GetMapping("/getHospitalSet/{id}")
    public Result<HospitalSet> getHospitalSetById(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.success(hospitalSet);
    }

    @ApiOperation("修改医院设置")
    @PutMapping("/updateHospitalSet")
    public Result updateHospitalSetById(@RequestBody HospitalSet hospitalSet) {
        hospitalSetService.updateHospitalSetById(hospitalSet);
        return Result.success();
    }

    @ApiOperation("批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.batchRemoveHospitalSet(idList);
        return Result.success();
    }


    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable boolean status) {
        Integer updateNumber = hospitalSetService.lockHospitalSet(id, status);
        return Result.state(updateNumber > 0);
    }

    @ApiOperation("发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hospitalCode = hospitalSet.getHospitalCode();
//        todo发送短信
        return Result.success();

    }

//    @ApiOperation("test")
//    @GetMapping("test")
//    public Result test() {
//        try {
//            try {
//                TimeUnit.SECONDS.sleep(2);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println(Thread.currentThread().getName());
//            THREAD_LOCAL.set(System.currentTimeMillis());
//            return Result.success(THREAD_LOCAL.get());
//        } finally {
//            THREAD_LOCAL.remove();
//        }
//    }

}
